﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;



namespace Steen.L.Knudsen
{   
    [ToolboxBitmap(typeof(resfinder), "BetterBindingSource.bmp")]
    class BetterBindingSource : BindingSource
    {

        [
        Browsable(false),
        Category(""),
        Description("DataSourceType")
        ]
        public Type DataSourceType { get; set; }


        public String _dataSourceText = "";
        [
            Browsable(true),
            RefreshPropertiesAttribute(RefreshProperties.All),
            Category(""),
            Description("DataSourceText")
        ]
        public String DataSourceText
        {
            get
            {
                return _dataSourceText;
            }
            set
            {
                _dataSourceText = value;
                
                if (this.DesignMode && _dataSourceText != "")
                {
                    try
                    {
                        DataSourceType = System.Type.GetType(_dataSourceText);
                        if (DataSourceType == null)
                        {
                            Error = "Error type not found!";
                            Type t = Find(_dataSourceText);
                            DataSourceType = t;
                            if (t != null)
                            {
                                _dataSourceText = t.FullName;
                                _dataSourceText = "";
                            }
                            else
                            {
                                _dataSourceText = "";
                            }
                        }
                        this.DataSource = DataSourceType;
                        
                        Error = "";

                    }
                    catch (Exception ex)
                    {
                        
                        Error = "Error! " + ex.Message;
                        Type t = Find(_dataSourceText);
                        DataSourceType = t;
                        if (t != null)
                        {
                            _dataSourceText = t.FullName;
                        }
                        else
                        {
                            _dataSourceText = "";
                        }
                    }
                }

            }
        }

        [
        Browsable(false),
        Category(""),
        Description("Error")
        ]
        public String Error { get; set; }

        public BetterBindingSource()
            : base()
        {

        }
        public BetterBindingSource(IContainer container)
            : base(container)
        {

        }

        public BetterBindingSource(Object obj, String str)
            : base(obj, str)
        {

        }


        public Type Find(string className)
        {
            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach(Assembly thisAsm in loadedAssemblies)
            {
                
                List<Type> types = thisAsm.GetTypes()
                    .Where(t => !t.IsAbstract && t.Name == className).ToList();

                if (types.Count == 1)
                {
                    return types.First();
                }

                types = thisAsm.GetTypes()
                    .Where(t => !t.IsAbstract && t.Name.Contains(className)).ToList();

                if (types.Count == 1)
                {
                    return types.First();
                }

                Debug.WriteLine("");
            }
            
            return null;
        }


    }

    internal class resfinder
    {

    }
}
